package cn.prophet.service.impl;

import cn.prophet.bean.*;
import cn.prophet.bean.vo.SuperResponseBean;
import cn.prophet.mapper.AccountDetailedMapper;
import cn.prophet.mapper.OpenAccountMapper;
import cn.prophet.mapper.UserMapper;
import cn.prophet.service.AccountDetailedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Slf4j
@Service
public class AccountDetailedServiceImpl implements AccountDetailedService {
    @Resource
    private AccountDetailedMapper accountDetailedMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private OpenAccountMapper openAccountMapper;

    @Override
    public void updateAccount(Byte type, BigDecimal money, String message, Long userId) {

        AccountDetailed accountDetailed = new AccountDetailed();
        accountDetailed.setCreateTime(new Date());
        accountDetailed.setIncomeMessage(message);
        accountDetailed.setMoney(money);
        accountDetailed.setUserId(userId);
        accountDetailed.setType(type);
        accountDetailedMapper.insertSelective(accountDetailed);


    }

    @Override
    public List<AccountDetailed> getAccountDetailedByUserId(Long userId) {
        AccountDetailedExample accountDetailedExample = new AccountDetailedExample();
        AccountDetailedExample.Criteria criteria = accountDetailedExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        accountDetailedExample.setOrderByClause("create_time desc");
        return accountDetailedMapper.selectByExample(accountDetailedExample);
    }

    /**
     * 开户映射
     * @param userId
     * @param antiWater
     * @param userName
     * @param userPsw
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SuperResponseBean<OpenAccount>  openAccount(long userId, BigDecimal antiWater, String userName, String userPsw,String withdraw_psw) {
        SuperResponseBean<OpenAccount> responseBean = new SuperResponseBean<>();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<User> list  = userMapper.selectByExample(userExample);
        if(list!=null&&list.size()>0){
            responseBean.setCode(0);
            responseBean.setMsg("该用户已注册！");
            return responseBean;

        }

        String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        OpenAccount openAccount = new OpenAccount();
        openAccount.setUniqueId(uuid);
        openAccount.setCreatetime(new Date());
        openAccount.setUserId(userId);
        openAccount.setAntiWater(antiWater);
        openAccount.setAccountName(userName);
        openAccountMapper.insertSelective(openAccount);
        User openUser = userMapper.selectByPrimaryKey(userId);
        User user = new User();
        user.setUserName(userName);
        user.setUserPsw(userPsw);
        user.setCreateTime(new Date());
        user.setBalance(new BigDecimal(1000));
        user.setWithdrawPsw(withdraw_psw);
        user.setGrade(openUser.getGrade()+1);
        userMapper.insertSelective(user);
        responseBean.setCode(1);
        responseBean.setMsg("开户成功");
        responseBean.setData(openAccount);
        return responseBean;
    }

    @Override
    public String inputAccount(String unique_id) {
        OpenAccountExample openAccountExample = new OpenAccountExample();
        OpenAccountExample.Criteria criteria = openAccountExample.createCriteria();
        criteria.andUniqueIdEqualTo(unique_id);
        List<OpenAccount> list = openAccountMapper.selectByExample(openAccountExample);
        if(list!=null&&list.size()==1){

            return list.get(0).getAccountName();
        }
        return null;
    }
}
